<?php
session_start();
require_once '../data/connect.class.php';
require_once '../data/pager.class.php';
$connect = new Connect("localhost","root","","email");
$param = $_POST['param'];

if (isset($param)) {
	if ($param=='list') {
		$url = $_SERVER['PHP_SELF'];
		$querySql="select aac.name as aname, mail_addr, subcripted, ama.name as name, domain  from admin_addr_class as aac,admin_mail_addr as ama where aac.id=ama.class_id;";
		$currentId = 1;
		$pageSize = 5;
		
		if (isset($_POST['currentId']) && $_POST['currentId'] > 0) {
			$currentId = $_POST['currentId'];
		}
	    if (isset($_POST['pageSize']) && $_POST['pageSize'] > 0) {
			$_SESSION['pageSize']=$_POST['pageSize'];
		}
		
	    if (isset($_SESSION['pageSize'])) {
			$pageSize = $_SESSION['pageSize'];
		} else {
			$_SESSION['pageSize'] = $pageSize;
		}
		$array = array();
		$array['pageSize'] = $pageSize;
		$array['sql']=$querySql;
		$array['currentId']=$currentId;
		$array['db']=$connect;
		$pager = new Pager($array);
		
        $pageData = $pager->getPageData();
        $resultSet = $pageData['pageData'];
        echo "<label>每页显示数：</label>"; 
        echo "<select id='pagesize_list' onchange='user_manage(\"".$url."\",\"list\",1,this.value)'>";
        for ($i=1; $i < 5; $i++) {
        	$ps = $i * 5;
        	if ($pageSize == $ps) {
        		echo "<option value=".$ps." selected=selected>".$ps."</option>";
        	} else {
        		echo "<option value=".$ps.">".$ps."</option>";
        	}
        	
        }
		echo "</select><br/><br/>";
		
		echo "<div class='page_user_row'>";
		echo "  <div class='page_addr_content'>";
		echo "     <strong>用户名</strong>";
		echo "  </div>";
		echo "  <div class='page_addr_content'>";
		echo "    <strong>邮件地址</strong>";
		echo "  </div>";
		echo "  <div class='page_addr_content'>";
		echo "    <strong>分类</strong>";
		echo "  </div>";
		echo "  <div class='page_addr_content'>";
		echo "    <strong>是否订阅</strong>";
		echo "  </div>";
		echo "</div>";
		
	    foreach($resultSet as $res) {
			echo "<div class='page_user_row'>";
			echo "  <div class='page_addr_content'>".$res['name']."</div>";
			echo "  <div class='page_addr_content'>".$res['mail_addr']."</div>";
			echo "  <div class='page_addr_content'>".$res['aname']."</div>";
			if ($res['subcripted']) {
				echo "  <div class='page_addr_content'>是</div>";
			} else {
				echo "  <div class='page_addr_content'>否</div>";
			}
			echo "</div>";
		}
		echo "<div style='float:right;'>";
	    if (!$pageData['isFirstPage']) {
			echo "<div style='float:left;cursor:pointer;' onclick='user_manage(\"".$url."\",\"list\",1,\"\")'>首页</div>&nbsp;";
		}
		for ($i=1; $i < $pageData['totalPages']+1; $i++) {
			if ($i == $pageData['currentPage']) {
				echo "<div style='color:red;float:left'>[".$i."]</div>&nbsp;";
			} else {
				echo "<div style='float:left;cursor:pointer;' onclick='user_manage(\"".$url."\",\"list\",[".$i."],\"\")'>[".$i."]</div>&nbsp;";
			}
			
		}
	    if (!$pageData['isLastPage']) {
			echo "<div style='float:left;cursor:pointer;' onclick='user_manage(\"".$url."\",\"list\",".$pageData['totalPages'].",\"\")'>尾页</div>&nbsp;";
		}
		echo "</div>";
	}elseif ($param=='add') {
		echo "<label>用户名</label>"; 
		echo "<input type='text' id='add_name' value='' />"; 
		echo "<label>邮件地址</label>"; 
	    echo "<input type='text' id='add_addr' value='' />"; 
	    
	    $querySql="select * from admin_addr_class;";
	    $resultSet = $connect->Query($querySql);
	    
	    echo "<label>分类：</label>"; 
        echo "<select id='addr_class_list'>";
	    foreach ($resultSet as $res) {
	    	echo "<option value=".$res['id'].">".$res['name']."</option>";
	    }
	    echo "</select>";
	    echo "<label>订阅</label>"; 
	    echo "<input type='checkbox' id='add_subscript' checked=true>"; 
	    echo "<div onclick='addAddr()' style='cursor:pointer;'>确定</div>";
	}elseif ($param=='edit') {
		
		$url = $_SERVER['PHP_SELF'];
		$currentId = 1;
		$pageSize = 5;
		
	    $querySql="select aac.name as aname, mail_addr, subcripted, ama.name as name, domain , ama.id as id , aac.id as aid from admin_addr_class as aac,admin_mail_addr as ama where aac.id=ama.class_id;";
	    
	    if (isset($_POST['currentId']) && $_POST['currentId'] > 0) {
			$currentId = $_POST['currentId'];
		}
	    if (isset($_POST['pageSize']) && $_POST['pageSize'] > 0) {
			$_SESSION['pageSize']=$_POST['pageSize'];
		}
		
	    if (isset($_SESSION['pageSize'])) {
			$pageSize = $_SESSION['pageSize'];
		}
		
		$array = array();
		$array['pageSize'] = $pageSize;
		$array['sql']=$querySql;
		$array['currentId']=$currentId;
		$array['db']=$connect;
		$pager = new Pager($array);
		
        $pageData = $pager->getPageData();
        $resultSet = $pageData['pageData'];
        
		echo "<div class='page_user_row'>";
		echo "  <div class='page_addr_content'>";
		echo "     <strong>用户名</strong>";
		echo "  </div>";
		echo "  <div class='page_addr_content'>";
		echo "    <strong>邮件地址</strong>";
		echo "  </div>";
		echo "  <div class='page_addr_content'>";
		echo "    <strong>分类</strong>";
		echo "  </div>";
		echo "  <div class='page_addr_content_small'>";
		echo "    <strong>是否订阅</strong>";
		echo "  </div>";
		echo "  <div class='page_addr_content_small'>";
		echo "    <strong>操作</strong>";
		echo "  </div>";
		echo "</div>";
		
	    foreach($resultSet as $res) {
			echo "<div class='page_user_row' id='page_addr_".$res['id']."'>";
			echo "  <div class='page_addr_content'>";
			echo $res['name'];
			echo "  </div>";
			echo "  <div class='page_addr_content'>";
			echo $res['mail_addr'];
			echo "  </div>";
			echo "  <div class='page_addr_content'>".$res['aname']."</div>";
			if ($res['subcripted']) {
				echo "<div class='page_addr_content_small'>";
                echo "是";
				echo "</div>";
			} else {
				echo "<div class='page_addr_content_small'>";
                echo "否";
				echo "</div>";
			}
			echo "  <div class='page_addr_content_small'>";
			echo "    <div onclick='editAddr(\"";
			echo $res['id']."\",\"";
			echo $res['name']."\",\"";
			echo $res['mail_addr']."\",";
			echo $res['aid'].",";
			echo $res['subcripted'].")' style='cursor:pointer;float:left;margin-left:15px'>编辑</div>";
			echo "    <div onclick='delAddr(\"".$res['id']."\")' style='cursor:pointer;'>删除</div>";
			echo "  </div>";
			echo "</div>";
		}
		
		echo "<div style='float:right;'>";
	    if (!$pageData['isFirstPage']) {
			echo "<div style='float:left;cursor:pointer;' onclick='user_manage(\"".$url."\",\"edit\",1,\"\")'>首页</div>&nbsp;";
		}
		for ($i=1; $i < $pageData['totalPages']+1; $i++) {
			if ($i == $pageData['currentPage']) {
				echo "<div style='color:red;float:left'>[".$i."]</div>&nbsp;";
			} else {
				echo "<div style='float:left;cursor:pointer;' onclick='user_manage(\"".$url."\",\"edit\",".$i.",\"\")'>[".$i."]</div>&nbsp;";
			}
			
		}
	    if (!$pageData['isLastPage']) {
			echo "<div style='float:left;cursor:pointer;' onclick='user_manage(\"".$url."\",\"edit\",".$pageData['totalPages'].",\"\")'>尾页</div>&nbsp;";
		}
		echo "</div>";
	}
}

?>